Y8 - SP2.1 - Getting Started with Python in VSCode

⏱️ Do It Now

Create a OneNote Page and copy this into the title:

Spring 2 - Lesson 1 - Introducing Python



Copy this Into a OneNote Page and complete it

📝 Do It Now

Think of one security risk you learned about last half term

Do each of the following:

  • Explain how it works
  • Describe the problems it causes
  • Explain how it can be prevented / stopped


🎯 Learning Objectives

  • We will understand how to create Python projects with VSCode and will learn how to adapt the interface to suit our needs
  • We will learn how to output text to the terminal with the print() function; making appropriate use of fStrings and comments.
  • We will learn how to create and assign values to a variable. Following this we will be able to output a variable to the terminal within an fString.

🐍 What is Python?

Python is a programming language used for a wide range of different purposes:

  • It has been used since the 1990s to manage the interactivity of websites; for example when users log in and when they submit information through online forms
  • Python is regularly used by scientists to perform calculations on vast amounts of research data
  • It has most recently been used to develop Large Language Models (LLMs) such as ChatGPT

🎓 Using VS Code

  • VSCode is a powerful online application we can use to edit and run Python code
  • It allows us to create and share our code projects; ultimately enabling an interactive classroom experience.

Click this link: VSCODEEDU.COM

Sign up with your Microsoft Account

Slide Title

Copy and paste this table to your OneNote page:

📝 Link To My Project

Paste a link to your project Python Lesson SP2.1 below:





Your teacher will show you how to set up and share your project

Click to see how you can set up and share a project


Paste the shareable link into your OneNote page

🎨🖌️Customising Your Interface

  • VSCode offers users the ability to customise their interface
  • This means complete control over font size and colour theme
  • Your teacher will now demonstrate how this is done!


Click to see how this is done!

🖨️ The Print Function

  • The first bit of coding we will use today is the print() function
  • This allows us to output something to the terminal (where we see the result of our code)

Write this code into your file and run it:

# This is an example of the print function
# It has been used to output the text "Hello World"
print("Hello World!")

💬Comments

  • The lines that begin with hashtags (#) are comments
  • They aren't execuatble code (python ignores them)
  • We use them to explain what certain sections of code do
  • Comments are important for both you and others to understand the code
# This is an example of the print function
# It has been used to output the text "Hello World"
print("Hello World!")

📱 Use Print to Output Calculations

The print() function can also be used to output the result of a mathematical calculation

Add this code to your file

# This outputs 5 multiplied by 2
print(5 * 2)

Run the code and the terminal should output 10

📱 Calculate within an fString

  • Python allows us to use something called an fString
  • Normal text appears as you type it, however a calculation can be placed between a set of curly brackets
  • The result of the calculation (between the curly brackets) will be displayed (instead of the calculation) when we run the program
  • Make sure you place an f before the speech marks at the beginning!

See the example below:

# Below is an example of using an fString to carry out a calculation
print(f"5 X 2 = {5 * 2}")

⌨️ Independent Activity 1


Copy and paste this comment above the activity you are about to complete
# Independent Activity 1


The Task:

  • Use an fString to output a calculation for each of the following operations
  • Use a separate print() for each calculation
  • Don't forget to comment your code on the line above!
Click to see the main mathematical operators
+ is for addition
/ is for division
* is for multiplication
- is for subtraction


Extension Activity
  • Research (online) how to calculate powers e.g. squaring and cubing
  • Carry out a squaring or cubing operation

📖 Variables


Variables are named pieces of memory:

  • We use them to store data such as text and numbers
  • We can then access them at a later point in a program
  • A variable must have a unique name

Below you can see an example of creating a variable and assigning a value to it (giving it a value)

# Creating a variable called name
# Assigning the value "bob" to the variable name
name = "bob"

Below you can see the variable being referenced (used) within an fString

# Outputting the value of the variable name within an fString
print(f"Hello {name} its nice to meet you.")

⌨️ Independent Activity 2

  • We are going to complete an independent activity, involving the use of variables
  • Firstly we must create a new python file!
  • Your teacher will show you this, however there is also a quick video guide below

Click to see how a separate file can be made

⌨️ Independent Activity 2


Copy and paste this comment above the activity you are about to complete
# Independent Activity 2


Create at least two additional variables

  • One can be the name of a place
  • One can be something a person does in that place


  • Output each in a separate fString
  • The fString will contain a sentence relevant to the variable
  • Make sure you comment your code!


The program should output something like this

Last year I went to Rome
When I was there I ate gnocci


Extension Activity
  • Create further variables
  • Provide appropriate fStrings for each to be included within

Plenary

📝 Reflection Task

Open the mini-whiteboard app in a new tab

Your teacher will start with these leading questions:

  • What is the function that allows us to output text to the terminal?
  • When using an fString, what do we put variables and calculations in?
  • What word means to give a value to a variable?